package form

import (
	"errors"
	"yak.admin/src/entity"
	"yak.admin/src/service"
)

type SmsForm struct {
	Phone    string `form:"phone" validate:"phone,required" chn:"手机号"`
	Scenes   string `form:"sms_scenes" validate:"required"`
	codeType int
	codeLen int
	codeCountdown int
}

func (form *SmsForm) Valid() error {
	if t, l, c := form.GetType(); t == -1 {
		return errors.New("不支持的业务场景")
	} else {
		form.codeType = t
		form.codeLen = l
		form.codeCountdown = c
	}
	return nil
}

func (form *SmsForm) GetType() (t int, len int, countdown int) {
	switch form.Scenes {
	case "reset-password":
		return entity.SmsCodeResetPassword, 6, 120
	case "register":
		return entity.SmsCodeRegister, 6, 120
	default:
		return -1, 0, 120
	}
}

func (form *SmsForm) SendCode() error {
	smsService := service.SmsService{}
	return smsService.SendCode(form.Phone, form.codeType, form.codeLen, form.codeCountdown)
}
